require 'utilities'
require 'encoding_conv'

class PlainWriter
  def write(o, courses, mailboxes)
    courses = EncodingConv.conv_courses("GBK", "UTF-8", courses)
    mailboxes = EncodingConv.conv_mailboxes("GBK", "UTF-8", mailboxes)
    if courses.instance_of? String
      o.puts courses + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    else
      courses.each do |course|
        o.puts course[:name] + "============================"
        
        o.puts " --HOMEWORKS--"
        course[:homeworks].each do |homework|
          o.print "\t"
          print_hash(o, homework)        
        end
        
        o.puts " --NOTICES--"
        course[:notices].each do |notice|
          o.print "\t"
          print_hash(o, notice)     
        end
        
        o.puts " --MATERIALS--"
        course[:materials].each do |material|
          o.print "\t"
          print_hash(o, material)
        end
      end
    end
    
    mailboxes.each do |mailbox|
      o.puts mailbox[:username] + " [#{mailbox[:type]}]: #{mailbox[:state]}" +"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
      mailbox[:mails].each do |mail|
        o.print "\t"
        print_hash(o, mail)
      end
    end
    
  end
  
  def write_to_file(file_name, courses, mailboxes)    
    File.open(file_name, "w") do |o|
      write(o, courses, mailboxes)
    end 
  end
end